package controle;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.JButton;

import abstraction.Cours;
import abstraction.Etudiant;
import abstraction.Note;

/** controleur qui rend les notes définitives
 * 
 * @author PICOSSON
 *
 */
public class ControlJButtonValidation implements ActionListener, Observer {

	private Cours modele;
	private JButton button;
	private Frame parent;

	public ControlJButtonValidation(Frame parent, Cours modele, JButton button) {
		this.modele = modele;
		this.button = button;
		this.parent = parent;
		button.setEnabled(false);
	}
	
	
	public void update(Observable o, Object message) {
		Integer iMessage = (Integer) message;
		if (iMessage == Cours.CHANGEMENT_VALIDATION)  {
			
			if (modele.validationPossible()){
				button.setEnabled(true);
			}
			else
				button.setEnabled(false);
		}
	}

	public void actionPerformed(ActionEvent arg0) {
		Vector<Etudiant> e = this.modele.findEtudiants();
		for(int i = 0; i < e.size(); i++){
			Note n = this.modele.findNote(e.get(i).getEmail());
			if(!n.estDefinitive()){

				System.out.println(n.getEmail() + "" + n.estDefinitive());
				n.setDefinitive(true);
				System.out.println(n.getEmail() + "" + n.estDefinitive());
				try {
					this.modele.saveNote(n, false);
				} catch (SQLException e1) {
					e1.printStackTrace();
					System.out.println(e1.getMessage());
				}
				n = this.modele.findNote(e.get(i).getEmail());

				System.out.println(n.getEmail() + "" + n.estDefinitive());
			}
			
		}
	}

}
